<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="68px">
         <el-form-item label="游戏名称" prop="roleName">
            <el-input
               v-model="queryParams.name"
               placeholder="请输入游戏名称"
               clearable
               style="width: 240px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
<!--         <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="handleAdd"
            >新增</el-button>
         </el-col>-->
        <el-col :span="1.5">
          <el-button
              type="primary"
              plain
              icon="Plus"
              @click="open=true"
          >新增</el-button>
        </el-col>

         <el-col :span="1.5">
            <el-button
               type="danger"
               plain
               icon="Delete"
               :disabled="multiple"
               @click="handleDelete"
            >删除</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="warning"
               plain
               icon="Download"
               @click="handleExport"
            >导出</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <!-- 表格数据 -->
      <el-table v-loading="loading" :data="gameList" @selection-change="handleSelectionChange">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="游戏id" align="center" prop="gameId" />
         <el-table-column label="游戏名称" prop="name" :show-overflow-tooltip="true" width="150" />
         <el-table-column label="版本" align="center" prop="version" />
         <el-table-column prop="gameType" label="游戏类型" width="200">
           <template #default="scope">
             <dict-tag :options="sys_game_type" :value="scope.row.gameType" />
           </template>
         </el-table-column>
         <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
<!--              <el-tooltip content="修改" placement="top" v-if="scope.row.roleId !== 1">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:role:edit']"></el-button>
              </el-tooltip>-->
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
              </el-tooltip>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />

      <!-- 添加或修改角色配置对话框 -->
      <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
         <el-form ref="gameRef" :model="form" :rules="rules" label-width="100px">
           <el-form-item label="选择游戏" prop="gameIds">
             <el-checkbox-group v-model="form.gameIds">
               <div
                   style="
        max-height: 400px;
        overflow-y: auto;
        border: 1px solid #eee;
        padding: 10px;
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 15px;
      "
               >
                 <el-checkbox
                     v-for="game in allGameList"
                     :key="game.gameId"
                     :label="game.gameId"
                     style="display: flex; align-items: center;"
                 >
                   <div>
                     <div>{{ game.name }} ({{ game.version }})</div>
                     <div style="font-size: 12px; color: #999; padding-top: 5px">
                       {{ getGameTypeLabel(game.gameType) }}
                     </div>
                   </div>
                 </el-checkbox>
               </div>
             </el-checkbox-group>
           </el-form-item>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>

   </div>
</template>

<script setup name="Role">
import { getGameList, updateGames, delGame } from "@/api/shop/project"
import { allGames } from "@/api/system/game"


const router = useRouter()
const { proxy } = getCurrentInstance()
const { sys_game_type } = proxy.useDict("sys_game_type")

const gameList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const allGameList = ref([])

/** 数据范围选项*/
const dataScopeOptions = ref([
  { value: "1", label: "全部数据权限" },
  { value: "2", label: "自定数据权限" },
  { value: "3", label: "本部门数据权限" },
  { value: "4", label: "本部门及以下数据权限" },
  { value: "5", label: "仅本人数据权限" }
])

const data = reactive({
  open: false,
  title: '',
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined
  },
  rules: {
    name: [{ required: true, message: "游戏名称不能为空", trigger: "blur" }]
  },
})

const { queryParams, form, rules } = toRefs(data)

function getGameTypeLabel(value) {
  const item = this.sys_game_type.find(t => t.value === value)
  return item ? item.label : value
}


/** 查询游戏列表 */
function getList() {
  loading.value = true
  getGameList({
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize,
    name: queryParams.value.name
  }).then(response => {
    gameList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 查询所有游戏 */
function getAllList() {
  allGames().then(response => {
    allGameList.value = response.data
    console.log(allGameList)
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _gameIds = row.gameId || ids.value
  console.log(ids.value)
  proxy.$modal.confirm('是否确认删除游戏编号为"' + _gameIds + '"的数据项?').then(function () {
    return delGame(_gameIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/role/export", {
    ...queryParams.value,
  }, `role_${new Date().getTime()}.xlsx`)
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.gameId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    gameIds: []
  }
  proxy.resetForm("gameRef")
}

// 提交表单
function submitForm() {
  if (form.value.gameIds.length != 0) {
    updateGames(form.value.gameIds).then(response => {
      proxy.$modal.msgSuccess("修改成功")
      open.value = false
      getList()
    })
  }
}

// 取消
function cancel() {
  open.value = false
  reset()
}

getAllList()
getList()
</script>
